﻿#pragma once


#include <QMainWindow>
#include <QTime>
#include <QTimer>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QDir>
#include <QMessageBox>
#include <QWidget>
#include <QApplication>
#include <QHeaderView>
#include <QMenuBar>
#include <QPushButton>
#include <QTableWidget>
#include <QTextEdit>
#include <QStringList>
#include <QFileDialog>
#include <QSettings>
#include "QDesktopServices"
#include "QUrl"
#include "QListView"
#include "QStringListModel"
#include "QDesktopServices"
#include "QStringListModel"
#include <QProcess>
#include <QDockWidget>
#include <QDateTime>
#include <QTcpSocket>
#include <QTranslator>
using namespace std;
#include <iostream>
#include <string.h>

#include "modbus.h"
#include "save_image.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
extern std::shared_ptr<spdlog::logger> logger ;

#include "recipe_dialog.h"

#include "camera_qdialog.h"
#include "qt_hread_process.h"
#include "login_dialog.h"
#include "show_qlabel.h"
#include "my_table_widget.h"
#include "commonfunction.h"
#include <QStandardItemModel>



#include "qdialog_user.h"

#include <mutex>
extern QImage matToQImage(const Mat mat_in);



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

        std::shared_ptr<spdlog::logger> logger_user  = spdlog::daily_logger_mt("logger_user", "logger_user/logger_user.txt", 0, 00);

    int m_camera_number=0;///<成员变量m_camera_number,该视觉系统中，相机的总数量



    vector<list<uint>> m_result_data;
    QVector<uint>          m_vector_time;

    //存图线程
    SaveImageThread* m_save_image_thread;
    QString m_save_image_dir;

    //主界面 统计数据显示
    QLabel* m_qlabel_time;

    QLabel* m_qlabel_cycle_time;



    QLabel* m_qlabel_qualified_quantity;
    atomic<uint64> m_qualified_quantity = 0;

    QLabel* m_qlabel_defective_quantity;
    atomic<uint64> m_defective_quantity =0 ;

    QLabel* m_qlabel_operating_time;


    QDateTime  m_qtime_start;


    QLabel* m_qlabel_camera0;

    QLabel* m_qlabel_camera1;

    QLabel* m_qlabel_camera2;

    QLabel* m_qlabel_camera3_0;
    QLabel* m_qlabel_camera3_1;
    QLabel* m_qlabel_camera3_2;

    QLabel* m_qlabel_camera4_0;
    QLabel* m_qlabel_camera4_1;
    QLabel* m_qlabel_camera4_2;

    //主界面 表格显示
    QDockWidget* m_qdockwidget_show_table= nullptr;
    QTableView *m_show_table;
    QStandardItemModel* m_show_table_data;

    //主界面 图像显示
    QWidget* m_image_widget;
    vector< ShowQLabel*> m_vector_show_qlabel;
    //检测方法设置界面
    QDialog* m_qdialog_detection = nullptr;
    QTabWidget* m_tabwidget_detection = nullptr;
    vector<QtThreadProcess*>  m_vector_detection_thread;

    //相机设置界面
    QDialog* m_qdialog_camera = nullptr;
    QTabWidget* m_tabwidget_camera = nullptr;
    vector<CameraBaseQDialog*> m_vector_camera;

    int m_online=0;///在线连接相机检测时，该值为1，离线检测时，该值为0

    QSettings* m_settings_para = nullptr;
    QString m_para_dir;
    QString m_exe_directory;
    QString m_type_dir = "type";
    QString m_type_name= "";
    QString m_save_data_dir="";

    // dockwidget pannel
    QDockWidget* m_qdockwidget_pannel= nullptr;
    QPushButton *m_qpushbutton_start_detection  = nullptr;
    QPushButton *m_qpushbutton_stop_detection  = nullptr;
    QPushButton *m_qpushbutton_para  = nullptr;
    QPushButton *m_qpushbutton_camera  = nullptr;
    QPushButton *m_qpushbutton_open_image_dir  = nullptr;
    QPushButton *m_qpushbutton_open_log_dir  = nullptr;
    QPushButton *m_qpushbutton_open_type_dir  = nullptr;
    QPushButton *m_qpushbutton_save_type_dir  = nullptr;
    QPushButton *m_qpushbutton_open_help  = nullptr;
    QPushButton *m_qpushbutton_exit  = nullptr;

    // dockwidget m_qdockwidget_devicesizetable
    QDockWidget* m_qdockwidget_devicesizetable= nullptr;

    //modibus
    modbus_t *m_modbus_tcp;
    QString  m_modbus_ip;
    int      m_modbus_port;
    QTimer*  m_timer_plc  = nullptr;
    QStringList m_save_data_time;
    vector<int> m_plc_trigger;
    vector<int> m_plc_result;
    int m_plc_write =0;
    int m_plc_result1 =0;
    int m_plc_result2 =0;

    //登录对话框
    QString m_admin_passeard = "19930810";
//    int my_para.m_login_privilege = 0;
    Login_Dialog* m_login_dialog=nullptr;

    QTimer*  m_timer_data = nullptr;

    UserManager* m_qwidget_user= nullptr;
    UserManager* m_qwidget_admin = nullptr;

    //Recipe Dialog
    StoreRecipeDialog m_store_recipe_dialog;

    LoadRecipeDialog  m_load_recipe_dialog;



public :
    void login();

    void startDetection();

    void stopDetection();

    void clickPara();

    void clickCamera();

    void clickUser();

    void clickAdmin();

    void closeSoftware();

    void returnMainFrame();

    virtual void closeEvent(QCloseEvent* event) override;

    void showResult(ShowResult result);

    void timerLoadImage();

    void timerData();

    void timerPLC();

    bool loadPara();

    void initdockPannel();

    void initMainWindow();

    void initImageControl();

    void initShowTable();

    void saveImage(ShowResult result);

    void loadFont();

    void setFont();

    void initQSQLITE(QString path)
    {
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

        if (!db.isValid()) {
              QMessageBox::critical(nullptr, path, "SQLite Driver not loaded!");
              return;
           }

        db.setDatabaseName(path);
        if (!db.open()) {
            QMessageBox::critical(nullptr, path, "SQLite open error");
            return;
        }
    }

    void storeRecipe();
    void loadRecipe();
};

#pragma execution_character_set("utf-8")
